<%--
  Created by IntelliJ IDEA.
  User: carol
  Date: 2018/6/16
  Time: 20:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.*,java.sql.*,javax.servlet.*,functest.*"%>
<html>
<head>
    <link type="text/css" rel="stylesheet" href="layui/css/layui.css" media="all">
    <script type="text/javascript" src="layui/layui.js"></script>
    <meta charset="UTF-8">
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    FindAPrint func =new FindAPrint();
    String city_id = request.getParameter("cityid");
    String port_name = request.getParameter("portname");
    String port_new = request.getParameter("portnew");
    String action = request.getParameter("action");
    if (city_id == null){//未传入cityid直接返回
        return;
    }
    else if(port_name == null) {//未提交机场名则输出一个表单
        String city_oldname = request.getParameter("cityoldname");
%>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>编辑城市机场</legend>
</fieldset>
<form id="editcity" method="post" class="layui-form" style="margin: 20px" action="editcity.jsp">
    <div class="layui-form-item">
        <label class="layui-form-label">城市ID</label>
        <div class="layui-input-block">
            <input type="text" name="cityid" readonly class="layui-input" value="<%=city_id%>">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">城市名称</label>
        <div class="layui-input-block">
            <input type="text" readonly class="layui-input" value="<%=city_oldname%>">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择机场</label>
        <div class="layui-input-block">
            <select name="portname" lay-verify="required">
                <option value=""></option>
                <%
                    List<String> portname=func.Findjcbyid(city_id);
                    for(String attribute : portname) {
                        out.println("<option value=\""+attribute+"\">"+
                                attribute+"</option>");
                    }
                %>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center;">
        <label class="layui-form-label">操作</label>
        <button type='button' class="layui-btn layui-btn-primary" onclick="change()">重命名</button>
        <button type='button' class="layui-btn layui-btn-danger" onclick="deleteport()">删除</button>
    </div>
    <div id="rename" class="layui-form-item" style="display: none;text-align: center">
        <label class="layui-form-label">机场重命名</label>
        <div class="layui-input-block">
            <input type="text" name="portnew" lay-verify="portnew" class="layui-input" placeholder="请输入" autocomplete="on">
        </div>
        <button style="margin-top: 15px" class="layui-btn" lay-submit="">提交</button>
    </div>
</form>
<%
    }
    else if (action != null){//传入action值说明是删除操作
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement = (Statement) connection.createStatement();  //连接实例
        String jcid = func.Findjc(port_name);
        String checksql = "select count(*) as total from hd where JC_START=" + jcid + " or JC_END=" + jcid + ";";
        ResultSet rst1 = statement.executeQuery(checksql);
        if (rst1.next()){
            int hdtotal = rst1.getInt("total");
            if (hdtotal > 0) {
                %>
                <blockquote class="layui-elem-quote layui-quote-nm">无法删除，尚有航段经过此机场</blockquote>
                <%
            }
            else {
                String delsql = "delete from jc where JC_DM=" + jcid + ";";
                statement.executeUpdate(delsql);
                %>
                <blockquote class="layui-elem-quote layui-quote-nm">删除成功</blockquote>
                <%
            }
        }
    }
    else{//否则就是重命名
        String driver="com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/hbpw?useUnicode=true&characterEncoding=utf-8&useSSL=false";//地址
        String user="hbpwuser";
        String pass="LFFLXY";
        Class.forName(driver);                   //加载jdbc驱动
        Connection connection  = (Connection) DriverManager.getConnection(url,user,pass);

        Statement statement = (Statement) connection.createStatement();  //连接实例
        String jcid = func.Findjc(port_name);
        Statement statement2 = (Statement) connection.createStatement();  //连接实例
        String selectsql2 = "select count(*) as checkname from jc where JC_MC='" + port_new + "';";
        ResultSet rst2 = statement2.executeQuery(selectsql2);
        rst2.next();
        int checkport = rst2.getInt("checkname");
        if (checkport > 0){
        %>
<blockquote class="layui-elem-quote layui-quote-nm">已有重名机场！</blockquote>
<%
        }
        else{
            String updatesql = "update jc set JC_MC='" + port_new + "' where JC_DM=" + jcid + ";";
            try {
                statement.executeUpdate(updatesql);
%>
<blockquote class="layui-elem-quote layui-quote-nm">修改成功</blockquote>
<%
            }catch (SQLException e){
                out.println("信息有误重新输入");
            }
        }
    }
%>
</body>
</html>
<script type="text/javascript">
    function change() {
        document.getElementById('rename').style.display='block';
        console.log('change');
    }
    function deleteport() {
        document.getElementById('editcity').action="editcity.jsp?action=del";
        document.getElementById('editcity').submit();
        console.log('submit');
    }
    layui.use(['form','layer'],function (){
        var form = layui.form
            ,layer = layui.layer;
        form.render();
        form.verify({
            portnew: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '请输个正常的名称';
                }
            }
        });
        form.render();
    })
</script>
